package com.koocloud.electroplatemanage.auth.controller;


import com.koocloud.electroplatemanage.auth.service.IUserService;
import com.koocloud.electroplatemanage.auth.service.impl.LoginService;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.common.util.CommonUtils;
import com.koocloud.electroplatemanage.pojo.User;
import com.koocloud.electroplatemanage.utils.Md5TokenGenerator;
import com.koocloud.electroplatemanage.utils.SecondCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    private static Logger log = LoggerFactory.getLogger("chronicdefense");

    @Resource
    LoginService loginService;
    @Autowired
    IUserService IUserService;

    @Autowired
    Md5TokenGenerator tokenGenerator;

    @RequestMapping("/toWelcome")
    @ResponseBody
    public String toWelcome(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {

        String token = request.getHeader("token");

        System.out.println("token1111111111:" + token);

        return "welcome";
    }

    /**
     * 单体服务 登录调用这个接口
     *
     * @param request
     * @param response
     * @param model
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public ResponseTemplate login(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model, String username, String password) {
        System.out.println(username);
        System.out.println(password);
        Map<String, Object> map = new HashMap<>();
        User user = loginService.getUserById(username);
        if(user.getEnableFag()==1){return ResponseTemplate.builder().code(2).message("用户已被禁用").data(null).build();}
        if (user.getPassword().equals(CommonUtils.md5(password))) {
            //获取当前人拥有的菜单
//            Map<String, Object> findMenu = loginService.findMenu(user.getUsername());
            map.put("token", user);
//            map.put("auth", findMenu);
//            request.getSession().setAttribute("auth", findMenu);
            user.setPassword("");
            request.getSession().setAttribute("token", user);
            return ResponseTemplate.builder().code(0).message("请求成功").data(map).build();
        } else {
            return ResponseTemplate.builder().code(1).message("用户名或密码输入错误").data(map).build();
        }

    }

    /**
     * 生成获取当前登录人所拥有权限页面操作按钮权限
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findTreeBtn", method = RequestMethod.POST, name = "生成权限树的方法")
    @ResponseBody
    public Map<String, Object> findTreeBtn(String username) {
        log.info("开始生成获取当前登录人所拥有权限页面操作按钮权限");
        Map<String, Object> map = loginService.findTreeBtn(username);
        log.info("生成获取当前登录人所拥有权限页面操作按钮权限结束");
        return map;
    }


    /**
     * 生成菜单树的方法（弃用）
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findMenu", method = RequestMethod.POST, name = "生成菜单树的方法")
    @ResponseBody
    public Map<String, Object> findMenu(String username) {
        Map<String, Object> map = new HashMap<>();
        log.info("开始生成权限树的方法");
        User selectUserByToken = IUserService.selectUserByToken(username);
        System.out.println(selectUserByToken.toString());
        if (StringUtils.isEmpty(selectUserByToken)) {
            map.put("code", 1);
            map.put("message", "根据token得到的用户信息为空");
        } else {
            map = loginService.findMenu(selectUserByToken.getUsername());
            map.put("code", 0);
        }
        log.info("生成权限树的方法结束");
        return map;
    }

    /**
     * 用户退出
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value = "/logout")
    @ResponseBody
    public ResponseTemplate logOut(HttpServletRequest request, HttpServletResponse response) {
        if (!StringUtils.isEmpty(request.getSession())) {
            request.getSession().removeAttribute("auth");
            request.getSession().removeAttribute("token");
        }
        return ResponseTemplate.builder().code(0).message("退出成功").build();
    }

    @RequestMapping(value = "/jobNumCheck")
    @ResponseBody
    public ResponseTemplate jobNumCheck(String jobNum) {
        if(!StringUtils.isEmpty(jobNum)&&SecondCheck.jobNumCheck(jobNum)) {
            return ResponseTemplate.builder().code(0).message("工号符合").build();
        }
        else {
            return ResponseTemplate.builder().code(1).message("工号首位必须大写,其余为数字，长度为7位").build();
        }
    }


}
